package com.zf.yichat.service.impl;

import com.zf.yichat.mapper.MemberLevelMapper;
import com.zf.yichat.mapper.MemberLevelPrivilegeMapper;
import com.zf.yichat.model.LotteryPrize;
import com.zf.yichat.model.MemberLevel;
import com.zf.yichat.model.MemberLevelPrivilege;
import com.zf.yichat.service.MemberLevelService;
import com.zf.yichat.utils.common.DtoChangeUtils;
import com.zf.yichat.utils.common.FsConst;
import com.zf.yichat.utils.response.FsResponse;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.Date;
import java.util.List;

@Service
public class MemberLevelServiceImpl implements MemberLevelService {

    @Autowired
    private MemberLevelMapper memberLevelMapper;

    @Autowired
    private MemberLevelPrivilegeMapper memberLevelPrivilegeMapper;

    @Override
    public MemberLevel selectLevelById(Long id) {
        return memberLevelMapper.selectByPrimaryKey(id);
    }

    @Override
    public FsResponse selectLevelList() {
        Example example = new Example(MemberLevel.class);
        example.createCriteria().andEqualTo("status", FsConst.Status.EFFECT);
        example.setOrderByClause("money asc");
        return DtoChangeUtils.getPageList(memberLevelMapper.selectByExample(example), data -> {
            MemberLevel level = new MemberLevel();
            BeanUtils.copyProperties(data, level);
            return level;
        });
    }

    @Override
    public void addMemberLevel(MemberLevel level) {
        level.setStatus(FsConst.Status.EFFECT);
        level.setCreateTime(new Date());
        memberLevelMapper.insertSelective(level);
    }

    @Override
    public void editMemberLevel(MemberLevel level) {
        memberLevelMapper.updateByPrimaryKeySelective(level);
    }

    @Override
    public void deleteMemberLevel(Integer id) {
        memberLevelMapper.deleteByPrimaryKey(id);
    }

    @Override
    public MemberLevelPrivilege selectLevelPrivilegeById(Long id) {
        return memberLevelPrivilegeMapper.selectLevelPrivilegeById(id);
    }

    @Override
    public void addMemberLevelPrivilege(MemberLevelPrivilege level) {
        level.setCreateTime(new Date());
        memberLevelPrivilegeMapper.addMemberLevelPrivilege(level);
    }

    @Override
    public void editMemberLevelPrivilege(MemberLevelPrivilege level) {
        memberLevelPrivilegeMapper.editMemberLevelPrivilege(level);
    }

    @Override
    public void deleteMemberLevelPrivilege(Integer id) {
        memberLevelPrivilegeMapper.deleteByPrimaryKey(id);
    }

    @Override
    public FsResponse selectLevelPrivilegeList(Long levelId) {
        List<MemberLevelPrivilege> list = memberLevelPrivilegeMapper.queryListByLevelId(levelId);
        return DtoChangeUtils.getPageList(list, data -> {
            MemberLevelPrivilege level = new MemberLevelPrivilege();
            BeanUtils.copyProperties(data, level);
            return level;
        });
    }
}
